package com.culturalCenter.dataCenter.Utils;


import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: ApiResult
 * @Description: 返回结果集封装
 * @Author: wu
 * @Date: 2020/06/12
 * @Version 1.0
 **/
@ApiModel("响应对象")
public class ApiResult<T> {

    /**
     * 响应码
     */
    @ApiModelProperty(value = "响应码")
    private String code;
    /**
     * 响应消息
     */
    @ApiModelProperty(value = "响应消息")
    private String msg;
    /**
     * 返回结果
     */
    @ApiModelProperty(value = "返回结果")
    private T data;

    private ApiResult() {
        this.code = ResultCode.SUCCESS.getCode();
        this.msg = ResultCode.SUCCESS.getMessage();
    }

    private ApiResult(T data) {
        this.code = ResultCode.SUCCESS.getCode();
        this.msg = ResultCode.SUCCESS.getMessage();
        this.setData(data);
    }

    private ApiResult(String code, String msg) {
        this.code = ResultCode.SUCCESS.getCode();
        this.msg = ResultCode.SUCCESS.getMessage();
        this.setCode(code);
        this.setMsg(msg);
    }

    private ApiResult(ResultCode code, T data) {
        this.code = code.getCode();
        this.msg = code.getMessage();
        this.data = data;
    }

    private ApiResult(ResultCode code) {
        this.code = code.getCode();
        this.msg = code.getMessage();
        this.data = null;
    }

    private ApiResult(String code, String msg, T data) {
        this.code = ResultCode.SUCCESS.getCode();
        this.msg = ResultCode.SUCCESS.getMessage();
        this.setCode(code);
        this.setMsg(msg);
        this.setData(data);
    }

    public static ApiResult instance() {
        return new ApiResult();
    }

    public static <T> ApiResult instance(T data) {
        return new ApiResult(data);
    }

    public static <T> ApiResult instance(ResultCode code) {
        return new ApiResult(code);
    }

    public static <T> ApiResult instance(ResultCode code, T data) {
        return new ApiResult(code, data);
    }

    public static <T> ApiResult instance(String code, String msg) {
        return new ApiResult(code, msg);
    }

    public static <T> ApiResult instance(String code, String msg, T data) {
        return new ApiResult(code, msg, data);
    }

    public ApiResult<T> setError(String code, String msg) {
        this.setCode(code);
        this.setMsg(msg);
        return this;
    }

    public ApiResult<T> setError(ResultCode errorCode) {
        this.setCode(errorCode.getCode());
        this.setMsg(errorCode.getMessage());
        return this;
    }

    public boolean isSuccess() {
        return (this.getCode().equals(ResultCode.SUCCESS.getCode()));
    }

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Map<String, Object> toJsonMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("data", this.data);
        map.put("msg", this.msg);
        map.put("code", this.code);
        return map;
    }

/**
 * 例子
 public ApiResult<User> getInfo()  {
 try {

 return ApiResult.instance(jdJrwCopyService.GetFindAll());
 } catch (Exception e) {
 log.error(e.getMessage(), e);
 }
 return null;

 }*/

}
